package com.zl.autocode.service.generator.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zl.autocode.common.constant.AutocodeConfigC;
import com.zl.autocode.common.constant.AutocodeEnum;
import com.zl.autocode.common.utils.generator.model.ConvertColumn;
import com.zl.autocode.common.utils.generator.model.ConvertConfig;
import com.zl.autocode.common.utils.generator.model.ConvertSequence;
import com.zl.autocode.common.utils.generator.model.ConvertTable;
import com.zl.autocode.common.utils.generator.util.JavaTypeConvert;
import com.zl.autocode.dao.autocode.ZLCfgGenerateDtlMapper;
import com.zl.autocode.dao.autocode.ZLCfgGenerateMapper;
import com.zl.autocode.dao.autocode.ZlDdlTemplateMapper;
import com.zl.autocode.dao.autocode.ZlDictDtlMapper;
import com.zl.autocode.dao.autocode.ZlDictMapper;
import com.zl.autocode.dao.autocode.ZlFieldMappingDtlMapper;
import com.zl.autocode.dao.autocode.ZlFieldMappingMapper;
import com.zl.autocode.dao.autocode.ZlKeywordDtlMapper;
import com.zl.autocode.dao.autocode.ZlKeywordMapper;
import com.zl.autocode.dao.autocode.ZlPreviewTemplateMapper;
import com.zl.autocode.dao.autocode.ZlProjectMapper;
import com.zl.autocode.dao.autocode.ZlProjectModuleMapper;
import com.zl.autocode.dao.autocode.ZlTableColumnMapper;
import com.zl.autocode.dao.autocode.ZlTableMapper;
import com.zl.autocode.dao.autocode.ZlTableSequenceMapper;
import com.zl.autocode.dao.autocode.ZlTemplateDtlMapper;
import com.zl.autocode.dao.autocode.ZlTemplateMapper;
import com.zl.autocode.dao.autocode.ZlValidationMapper;
import com.zl.autocode.dao.autocode.ZlWebComponentMapper;
import com.zl.autocode.dto.generator.ReqCodeGenereatorDTO;
import com.zl.autocode.dto.generator.ReqGetFirstTemplateOtherDTO;
import com.zl.autocode.dto.generator.ReqInitComponentTableDTO;
import com.zl.autocode.dto.generator.ReqLoadTableDTO;
import com.zl.autocode.dto.generator.ReqLoadTemplateDTO;
import com.zl.autocode.dto.generator.ResLoadTableDTO;
import com.zl.autocode.dto.generator.ResLoadTemplateDTO;
import com.zl.autocode.dto.generator.TemplateDtlVO;
import com.zl.autocode.entity.autocode.ZLCfgGenerate;
import com.zl.autocode.entity.autocode.ZLCfgGenerateDtl;
import com.zl.autocode.entity.autocode.ZlDdlTemplate;
import com.zl.autocode.entity.autocode.ZlDictDtl;
import com.zl.autocode.entity.autocode.ZlFieldMapping;
import com.zl.autocode.entity.autocode.ZlFieldMappingDtl;
import com.zl.autocode.entity.autocode.ZlKeyword;
import com.zl.autocode.entity.autocode.ZlKeywordDtl;
import com.zl.autocode.entity.autocode.ZlPreviewTemplate;
import com.zl.autocode.entity.autocode.ZlProject;
import com.zl.autocode.entity.autocode.ZlProjectModule;
import com.zl.autocode.entity.autocode.ZlTable;
import com.zl.autocode.entity.autocode.ZlTableColumn;
import com.zl.autocode.entity.autocode.ZlTableSequence;
import com.zl.autocode.entity.autocode.ZlTemplate;
import com.zl.autocode.entity.autocode.ZlTemplateDtl;
import com.zl.autocode.entity.autocode.ZlWebComponent;
import com.zl.autocode.entity.autocode.po.ZLCfgGenerateDtlVo;
import com.zl.autocode.entity.autocode.po.ZLCfgGenerateVo;
import com.zl.autocode.service.base.AutocodeBaseManager;
import com.zl.autocode.service.generator.IGeneratorService;
import com.zl.common.dto.ReqDTO;
import com.zl.common.enums.resp.GlobalRespEnum;
import com.zl.common.exception.BizException;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

/**
 *  
 * @author ZL
 */
@Service("autocodeGeneratorService")
public class GeneratorServiceImpl extends AutocodeBaseManager implements IGeneratorService {
    @Autowired
    private ZlDictMapper dictMapper;
    @Autowired
    private ZlDictDtlMapper dictDtlMapper;
    @Autowired
    private ZlFieldMappingMapper fieldMappingMapper;
    @Autowired
    private ZlFieldMappingDtlMapper fieldMappingDtlMapper;
    @Autowired
    private ZlKeywordMapper keywordMapper;
    @Autowired
    private ZlKeywordDtlMapper keywordDtlMapper;
    @Autowired
    private ZlProjectModuleMapper projectModuleMapper;
    @Autowired
    private ZlProjectMapper projectMapper;
    @Autowired
    private ZlTemplateDtlMapper templateDtlMapper;
    @Autowired
    private ZlTemplateMapper templateMapper;
    @Autowired
    private ZlTableColumnMapper tableColumnMapper;
    @Autowired
    private ZlTableMapper tableMapper;
    @Autowired
    private ZlTableSequenceMapper tableSequenceMapper;
    @Autowired
    private ZlValidationMapper validationMapper;
    @Autowired
    private ZLCfgGenerateMapper cfgGenerateMapper;
    @Autowired
    private ZLCfgGenerateDtlMapper cfgGenerateDtlMapper;
    @Autowired
    private ZlDdlTemplateMapper ddlTemplateMapper;
    @Autowired
    private ZlPreviewTemplateMapper previewTemplateMapper;
    @Autowired
    private ZlWebComponentMapper webComponentMapper;

    
    @Override
    public ResLoadTableDTO doInitialTables(ReqDTO<ReqLoadTableDTO> reqInfo,ConvertConfig iConfig)
            throws Exception {
        LOGGER.info("执行默认表解析方法");
        ReqLoadTableDTO reqData = reqInfo.getReqData();//请求
        
        ResLoadTableDTO resDTO = new ResLoadTableDTO();//返回
        Map<String,String> warnTables = new HashMap<>();
        
        
        Date time = getSystemTime().getDate();
        
        Map<String, Object> paramMap = new HashMap<>();//初始公用查询条件MAP
        
        //查询项目信息
        paramMap.put("projectId", reqData.getProjectId());
        ZlProject projectInfo =  projectMapper.selectOneByMap(paramMap);
        
        
        //查询数据库关键字
        paramMap.clear();
        paramMap.put("databaseType", projectInfo.getDatabaseType());
        ZlKeyword keywordInfo = keywordMapper.selectOneByMap(paramMap);
        if(keywordInfo==null){//没有则查通用all
            paramMap.put("databaseType", "all");
            keywordInfo = keywordMapper.selectOneByMap(paramMap);
        }
        List<String> keywordValList = new ArrayList<>();
        if(keywordInfo != null){
            paramMap.clear();
            paramMap.put("keywordId", keywordInfo.getKeywordId());
            List<ZlKeywordDtl> keywordDtlInfos =  keywordDtlMapper.selectListByMap(paramMap);
            keywordValList = keywordDtlInfos.stream()
                    .map(x -> { return StringUtils.lowerCase(x.getKeywordVal());})
                    .collect(Collectors.toList());
        }
        
        //查询映射明细
        paramMap.clear();
        paramMap.put("mappingId", projectInfo.getMappingId());
        ZlFieldMapping fieldMappingRecordInfo = fieldMappingMapper.selectOneByMap(paramMap);
        List<ZlFieldMappingDtl> fieldMappingDtlRecordInfos = fieldMappingDtlMapper.selectListByMap(paramMap);
        Map<String, String[]> fieldMappings = fieldMappingDtlRecordInfos.stream().collect(Collectors
                .toMap(ZlFieldMappingDtl::getMatchingRule,lt->{
                    String[] tmp = new String[5];
                    tmp[0] = lt.getJdbcType();
                    tmp[1] = lt.getJavaFullType();
                    tmp[2] = lt.getJavaType();
                    tmp[3] = lt.getJavaVoType();
                    tmp[4] = lt.getStandardFlag();
                    return tmp;
                }));
        
        //查询当前项目存在的所有表
        paramMap.clear();
        paramMap.put("projectId", reqData.getProjectId());
        List<ZlTable> tblRecords = tableMapper.selectListByMap(paramMap);
        Map<String, ZlTable> tblRecordMap = new HashMap<>();
        if(tblRecords!=null&&tblRecords.size()>0){
            tblRecordMap = tblRecords.stream().collect(Collectors.toMap(x->{return x.getTableCode().toLowerCase();}, lt->lt));
        }
        //创建时间统一更新为同步时间
        List<ConvertTable> tabls = iConfig.getTbls();
        if(ObjectUtil.isNotEmpty(tabls)){
            //表
            for(ConvertTable tlt:tabls){
                tlt.setTableCode(tlt.getTableCode().toLowerCase());
                ZlTable tblRecordInfo = tblRecordMap.get(tlt.getTableCode());
                boolean tableFlag = tblRecordInfo!=null?true:false;//表是否存在
                LOGGER.info("项目>>"+reqData.getProjectId() +"  表>>"+ tlt.getTableCode()+"  是否存在>>"+tableFlag);
                if(tableFlag){//用原来编码查
                    paramMap.clear();//用完清理
                    paramMap.put("projectId", reqData.getProjectId());
                    paramMap.put("tableCode", tblRecordInfo.getTableCode());
                }
                
                ZlTable tblRecord = new ZlTable();
                initCreateInfo(tblRecord, reqInfo, time);
                tblRecord.setTableCode(tlt.getTableCode());
                tblRecord.setTableName(tlt.getTableName());
                tblRecord.setTableComment(tlt.getTableComment());
                tblRecord.setProjectId(reqData.getProjectId());
                if(!tableFlag || "1".equals(reqData.getCoverFlag())){
                    tblRecord.setTableTag(tlt.getTableTag());//如果是从表中获取是没有该项的，定义规则从备注获取
                    tblRecord.setTableTitle(tlt.getTableTitle());
                }
                
                //扩展处理
                tblRecord.setTableAlias("a");
                tblRecord.setTableJavaFullCode(StrUtil.upperFirst(StrUtil.toCamelCase(StringUtils.lowerCase(tlt.getTableCode()))));//保留原始驼峰,首字母大写
                String simpleCode = tlt.getTableCode();
                if(StringUtils.isNotBlank(projectInfo.getPrefixOverride())){//去除前后缀
                    simpleCode = simpleCode.replaceAll(projectInfo.getPrefixOverride().toLowerCase(), "");
                }
                tblRecord.setTableJavaCode(StrUtil.lowerFirst(StrUtil.toCamelCase(simpleCode)));//首字母小写
                if (tableFlag) {
                    //保留原数据，非空
                    tblRecord.setTableId(tblRecordInfo.getTableId());
                    tableMapper.updateByPrimaryKeySelective(tblRecord);
                    
                    if("1".equals(tblRecordInfo.getLoadFlag())){
                      //处理表字段，表已存在则需要查询
                        doInitialTableColumn(fieldMappingRecordInfo.getMatchWay(),fieldMappings,paramMap,tblRecord,tlt.getAllcols(),reqInfo,iConfig, time,true, keywordValList, warnTables);
                    }
                } else {
                    tblRecord.setLoadFlag("1");//默认加载
                    tblRecord.setOrderNum(0L);
                    tblRecord.setTableId(getUniqueId());//主键
                    tableMapper.insertSelective(tblRecord);
                    
                    //处理表字段，表不存在则不需要查询
                    doInitialTableColumn(fieldMappingRecordInfo.getMatchWay(),fieldMappings,paramMap,tblRecord,tlt.getAllcols(),reqInfo,iConfig, time,false, keywordValList, warnTables);
                }
                
                if(warnTables.get(tblRecord.getTableCode()) == null){
                    warnTables.put(tblRecord.getTableCode(), "无");
                }
            }
        }
        
        List<ConvertSequence> seqs = iConfig.getSeqs();
        if(ObjectUtil.isNotEmpty(seqs)){
            seqs.stream().forEach(slt -> {
                //自增序列
                paramMap.clear();//用完清理
                paramMap.put("projectId", reqData.getProjectId());
                paramMap.put("sequenceCode", slt.getSequenceCode());
                ZlTableSequence seqRecordInfo = tableSequenceMapper.selectOneByMap(paramMap);
                boolean seqFlag = seqRecordInfo!=null?true:false;//序号是否存在
                
                ZlTableSequence seqRecord = new ZlTableSequence();
                initCreateInfo(seqRecord, reqInfo, time);
                seqRecord.setSequenceCode(slt.getSequenceCode());
                seqRecord.setSequenceMax(slt.getSequenceMax());
                seqRecord.setSequenceMin(slt.getSequenceMin());
                seqRecord.setSequenceTag(slt.getSequenceTag());
                seqRecord.setSequenceName(slt.getSequenceName());
                seqRecord.setSequenceComment(slt.getSequenceComment());
                
                seqRecord.setSequenceJavaCode(StrUtil.upperFirst(StrUtil.toCamelCase(StringUtils.lowerCase(slt.getSequenceCode()))));//保留原始驼峰,首字母大写
                
                if(seqFlag){
                    seqRecord.setSequenceId(seqRecordInfo.getSequenceId());//主键
                    tableSequenceMapper.updateByPrimaryKeySelective(seqRecord);
                }else {
                    seqRecord.setProjectId(reqData.getProjectId());
                    seqRecord.setOrderNum(0L);
                    seqRecord.setSequenceId(getUniqueId());//主键
                    tableSequenceMapper.insertSelective(seqRecord);
                }
            });
        }
        resDTO.setWarnTables(warnTables);
        return resDTO;
    }
    /** 处理表字段*/
    private void doInitialTableColumn(String matchWay, Map<String, String[]> fieldMappings,Map<String, Object> paramMap,ZlTable tblRecord,List<ConvertColumn> allcols
            ,ReqDTO<ReqLoadTableDTO> reqInfo,ConvertConfig iConfig,Date time,boolean existFlag,List<String> keywordValList,Map<String,String> warnTables){
        //字段
        if(allcols != null && allcols.size() > 0){
            
            Map<String, ZlTableColumn> colsMap = new HashMap<>();
            if(existFlag){
                List<ZlTableColumn> columnRecordInfos = tableColumnMapper.selectListByMap(paramMap);
                if(ObjectUtil.isNotEmpty(columnRecordInfos)){
                    tableColumnMapper.deleteByMap(paramMap);//全部删除保持排序ZlTableColumn::getColumnCode
                    colsMap = columnRecordInfos.stream().collect(Collectors.toMap(x->{return x.getColumnCode().toLowerCase();} ,Function.identity()));
                }
            }
            boolean noKeyflag=true;
            for(Integer coli=1,tbi=allcols.size(); tbi >0;coli++,tbi--) {
                ConvertColumn clt = allcols.get(tbi-1);
                clt.setColumnCode(clt.getColumnCode().toLowerCase());
                
                ZlTableColumn colRecord = new ZlTableColumn();
                initCreateInfo(colRecord, reqInfo, time);
                ZlTableColumn oldCol = colsMap.get(clt.getColumnCode());
                if (oldCol != null) {
                    //保留原数据，非空
//                    BeanUtil.copyProperties(colsMap.get(clt.getColumnCode()), colRecord, CopyOptions.create().setIgnoreNullValue(true));
                    colRecord.setColumnId(oldCol.getColumnId());
                    colRecord.setColumnTitle(oldCol.getColumnTitle());
                }else {
                    colRecord.setColumnId(getUniqueId());//主键
                    colRecord.setColumnTitle(clt.getColumnTitle());
                }
                if("1".equals(reqInfo.getReqData().getCoverFlag())){
                    colRecord.setColumnTitle(clt.getColumnTitle());
                }
                
                colRecord.setProjectId(tblRecord.getProjectId());
                colRecord.setTableCode(tblRecord.getTableCode());
                colRecord.setOrderNum(coli.longValue());//排序保持
                colRecord.setColumnName(clt.getColumnName());
                if(StringUtils.isBlank(colRecord.getColumnName())){
                    colRecord.setColumnName(clt.getColumnCode());
                }
                colRecord.setColumnCode(clt.getColumnCode());
                colRecord.setColumnComment(clt.getColumnComment());
                colRecord.setColumnDatatype(clt.getColumnDatatype());
                colRecord.setColumnLength(clt.getColumnLength());
                colRecord.setColumnPrecision(clt.getColumnPrecision());
                colRecord.setColumnPrimarykey(StringUtils.isNotBlank(clt.getColumnPrimarykey())?clt.getColumnPrimarykey():"0");
                colRecord.setColumnMandatory(StringUtils.isNotBlank(clt.getColumnMandatory())?clt.getColumnMandatory():"1");
                colRecord.setColumnDefault(clt.getColumnDefault());
                if(StringUtils.isBlank(colRecord.getColumnTitle())){
                    colRecord.setColumnTitle(colRecord.getColumnCode());
                }
                //扩展处理
                String camelCode = StrUtil.toCamelCase(colRecord.getColumnCode());
                colRecord.setColumnJavaCode(camelCode);
                
                String[] typearr = null;
                if("1".equals(matchWay)){//开始字符匹配
                   typearr = JavaTypeConvert.convertByPrefix(iConfig.getDatabaseType(),colRecord.getColumnDatatype(), fieldMappings);
                }else{//正则匹配
                   typearr = JavaTypeConvert.convertByPattern(iConfig.getDatabaseType(),colRecord.getColumnDatatype(), fieldMappings);
                }
                colRecord.setJdbcType(typearr[0]);
                colRecord.setJavaFullType(typearr[1]);
                colRecord.setJavaType(typearr[2]);
                colRecord.setJavaVoType(typearr[3]);
                colRecord.setStandardFlag(typearr[4]);
                
                //关键字处理
                if(keywordValList.contains(colRecord.getColumnCode()) 
                        || keywordValList.contains(colRecord.getColumnJavaCode())){
                    if(warnTables.get(tblRecord.getTableCode())!=null){
                        warnTables.put(tblRecord.getTableCode(), warnTables.get(tblRecord.getTableCode()) +", "+colRecord.getColumnCode()+"-"+colRecord.getColumnJavaCode());
                    }else{
                        warnTables.put(tblRecord.getTableCode(), colRecord.getColumnCode()+"-"+colRecord.getColumnJavaCode());
                    }
                }
                if("1".equals(colRecord.getColumnPrimarykey())){
                    noKeyflag=false;
                }
                
                tableColumnMapper.insertSelective(colRecord);
            }
            if(noKeyflag){
                String t = warnTables.get(tblRecord.getTableCode());
                if(t!=null){
                    warnTables.put(tblRecord.getTableCode(), t +"。警告：缺少主键字段！");
                }else{
                    warnTables.put(tblRecord.getTableCode(), "警告：缺少主键字段！");
                }
            }
        }
    }
    
    @Override
    public void queryProjectTableConfig(ReqDTO<ReqLoadTableDTO> reqInfo,ConvertConfig iConfig) throws Exception {
        ReqLoadTableDTO reqData = reqInfo.getReqData();
        
        Map<String, Object> projectMap = new HashMap<>();
        projectMap.put("projectId", reqData.getProjectId());
        ZlProject projectRecord =  projectMapper.selectOneByMap(projectMap);
        
        iConfig.setDatabaseType(projectRecord.getDatabaseType());
        iConfig.setJdbcLoginname(projectRecord.getJdbcLoginname());
        iConfig.setJdbcPassword(projectRecord.getJdbcPassword());
        iConfig.setJdbcUrl(projectRecord.getJdbcUrl());
        iConfig.setPdmPath(projectRecord.getPdmPath());
        iConfig.setSqlPath(projectRecord.getSqlPath());
        iConfig.setLoadType(StringUtils.isNotBlank(iConfig.getLoadType())?iConfig.getLoadType():projectRecord.getLoadType());
        iConfig.setCharacterEncoding(projectRecord.getCharacterEncoding());
    }
    
    @Override
    public ResLoadTemplateDTO doInitialTemplateDtl(ReqDTO<ReqLoadTemplateDTO> reqInfo) throws Exception {
        // TODO Auto-generated method stub
        ReqLoadTemplateDTO reqData = reqInfo.getReqData();
        ResLoadTemplateDTO resDTO = new ResLoadTemplateDTO();
        Date time = getSystemTime().getDate();
        
        if(reqData.getTemplateDtlList()!=null && reqData.getTemplateDtlList().size()>0){
            resDTO.setTemplateId(reqData.getTemplateDtlList().get(0).getTemplateId());
            Map<String,Object> paramMap = new HashMap<>();
            paramMap.put("templateId",resDTO.getTemplateId());
            List<ZlTemplateDtl> templateDtlList = templateDtlMapper.selectListByMap(paramMap);
            Map<String, ZlTemplateDtl> dtlMaps = new HashMap<>();
            if(templateDtlList != null && templateDtlList.size() > 0){
                templateDtlMapper.deleteByMap(paramMap);
                dtlMaps = templateDtlList.stream().collect(Collectors.toMap(ZlTemplateDtl::getFileTitle, Function.identity()));
            }
            for(TemplateDtlVO x :reqData.getTemplateDtlList()){
                ZlTemplateDtl record = new ZlTemplateDtl();
                BeanUtil.copyProperties(x, record);
                initCreateInfo(record, reqInfo, time);
                
                ZlTemplateDtl dtlvo = dtlMaps.get(x.getFileTitle());
                if(dtlvo != null){
                    record.setTemplateFileId(dtlvo.getTemplateFileId());
                    if(StringUtils.isBlank(record.getQuoteTag())){
                        record.setQuoteTag(dtlvo.getQuoteTag());
                    }
                    if(record.getOrderNum() == null){
                        record.setOrderNum(dtlvo.getOrderNum());
                    }
                    if(StringUtils.isBlank(record.getOutputFileFormat())){
                        record.setOutputFileFormat(dtlvo.getOutputFileFormat());
                    }
                    if(StringUtils.isBlank(record.getOutputType())){
                        record.setOutputType(dtlvo.getOutputType());
                    }
                }else{
                    record.setTemplateFileId(getUniqueId());
                }
                
                templateDtlMapper.insertSelective(record);
            }
            
            
        }
        
        return resDTO;
    }
    
    @Override
    public ZLCfgGenerateVo queryCfgGenerate(ReqDTO<ReqCodeGenereatorDTO> reqInfo) throws Exception {
        ReqCodeGenereatorDTO reqData = reqInfo.getReqData();
        
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("cfgGenerateId", reqData.getCfgGenerateId());
        ZLCfgGenerate cfgGenerateData = cfgGenerateMapper.selectOneByMap(paramMap);
        
        ZLCfgGenerateVo cfgGenerateDataVo = BeanUtil.copyProperties(cfgGenerateData, ZLCfgGenerateVo.class);
        
        if(reqData.getCfgGenerateDtlIds() != null && reqData.getCfgGenerateDtlIds().size() > 0){
            List<ZLCfgGenerateDtlVo> cfgGenerateDtlList = new ArrayList<>();
            for(String cfgGenerateDtlId : reqData.getCfgGenerateDtlIds()){
                paramMap.put("cfgGenerateDtlId", cfgGenerateDtlId);
                ZLCfgGenerateDtl cfgGenerateDtlInfo = cfgGenerateDtlMapper.selectOneByMap(paramMap);
                if(cfgGenerateDtlInfo != null){
                    cfgGenerateDtlList.add(BeanUtil.copyProperties(cfgGenerateDtlInfo, ZLCfgGenerateDtlVo.class));
                }
            }
            if(cfgGenerateDtlList.size() > 0){
                cfgGenerateDataVo.setCfgGenerateDtlList(cfgGenerateDtlList);  
            }
        }
        //查询项目信息
        paramMap.clear();
        paramMap.put("projectId", cfgGenerateDataVo.getProjectId());
        ZlProject projectInfo =  projectMapper.selectOneByMap(paramMap);
        cfgGenerateDataVo.setCharacterEncoding(projectInfo.getCharacterEncoding());
        cfgGenerateDataVo.setPrefixOverride(projectInfo.getPrefixOverride());
        cfgGenerateDataVo.setSuffixOverride(projectInfo.getSuffixOverride());
        cfgGenerateDataVo.setDatabaseType(projectInfo.getDatabaseType());
        cfgGenerateDataVo.setProjectTitle(projectInfo.getProjectTitle());
        //查询项目模块信息
        paramMap.clear();
        paramMap.put("moduleId", cfgGenerateDataVo.getModuleId());
        ZlProjectModule projectModuleInfo =  projectModuleMapper.selectOneByMap(paramMap);
        cfgGenerateDataVo.setBasePackagePath(projectModuleInfo.getBasePackagePath());
        cfgGenerateDataVo.setOutputPackagePath(projectModuleInfo.getOutputPackagePath());
        cfgGenerateDataVo.setOutputRootPath(projectModuleInfo.getOutputRootPath());
        cfgGenerateDataVo.setTagAuthor(projectModuleInfo.getTagAuthor());
        cfgGenerateDataVo.setTagExplain1(projectModuleInfo.getTagExplain1());
        cfgGenerateDataVo.setTagExplain2(projectModuleInfo.getTagExplain2());
        cfgGenerateDataVo.setModuleTitle(projectModuleInfo.getModuleTitle());
        
        //查询模板信息
        paramMap.clear();
        paramMap.put("templateId",cfgGenerateDataVo.getTemplateId());
        ZlTemplate templateInfo = templateMapper.selectOneByMap(paramMap);
        cfgGenerateDataVo.setTemplatePath(templateInfo.getTemplatePath());
        
        return cfgGenerateDataVo;
    }
    
    @Override
    public ResLoadTableDTO doInitialTablesByEntity(ReqDTO<ReqLoadTableDTO> reqInfo,ConvertConfig iConfig)
            throws Exception {
        LOGGER.info("执行默认表解析方法");
        return doInitialTables(reqInfo, iConfig);
    }
    
    /** 唯一id,自定规则 */
    private String getUniqueId() {
        return IdUtil.simpleUUID();
    }
    
    
    @Override
    public String getFirstTempateOther(ReqGetFirstTemplateOtherDTO reqData,String type) throws Exception {
        Map<String, Object> param = new HashMap<>();
        if(StringUtils.isNotBlank(reqData.getTemplateId())) {
            param.put("templateId", reqData.getTemplateId());
        }else if(StringUtils.isNotBlank(reqData.getModuleId())) {
            param.put("moduleId", reqData.getModuleId());
        }else if(StringUtils.isNotBlank(reqData.getProjectId())) {
            param.put("projectId", reqData.getProjectId());
        }
        if(AutocodeConfigC.PATH_CHILDREN_DDL.equals(type)) {
            List<ZlDdlTemplate> list = ddlTemplateMapper.selectListByMapForSelectItem(param);
            if(!list.isEmpty()) {
                ZlDdlTemplate t = list.get(0);
                for(ZlDdlTemplate x:list){
                    if(x.getTemplateId().equals(reqData.getTemplateId())) {
                        t=x;
                        break;
                    }
                }
                reqData.setTemplateId(t.getTemplateId());
                return t.getTemplatePath();
            }
        }else if(AutocodeConfigC.PATH_CHILDREN_PREVIEW.equals(type)) {
            List<ZlPreviewTemplate> list =  previewTemplateMapper.selectListByMapForSelectItem(param);
            if(!list.isEmpty()) {
                ZlPreviewTemplate t = list.get(0);
                for(ZlPreviewTemplate x:list){
                    if(x.getTemplateId().equals(reqData.getTemplateId())
                        || (StringUtils.isBlank(reqData.getTemplateId()) && StringUtils.isNotBlank(x.getModuleId()) && x.getModuleId().equals(reqData.getModuleId()))) {
                        t=x;
                        break;
                    }
                }
                reqData.setTemplateId(t.getTemplateId());
                return t.getTemplatePath();
            }
        }
        return null;
     }
    @Override
    public void doInitialProjectWebComponent(ReqDTO<ReqInitComponentTableDTO> reqInfo, String dictType)
            throws Exception {
         ReqInitComponentTableDTO reqData = reqInfo.getReqData();
         Date time = getSystemTime().getDate();
         
         Map<String, Object> paramMap = new HashMap<>();
         paramMap.put("dictType", dictType);
         paramMap.put("dataStatus", AutocodeEnum.DATA_STATUS.ENABLE.getCode());
         List<ZlDictDtl> ditctDtls = dictDtlMapper.selectListByMapForSelectItem(paramMap);
         if(ditctDtls == null || ditctDtls.isEmpty()){
             return;
         }
         for(ZlDictDtl dtl: ditctDtls){
             ZlWebComponent record = new ZlWebComponent();
             record.setProjectId(reqData.getProjectId());
             record.setComponentTitle(dtl.getDictDisplay1());
             record.setComponentType(dtl.getDictDtlNo());
             record.setComponentCode(dtl.getDictDtlNo());
             record.setOrderNum(dtl.getOrderNum());
             initCreateInfo(record, reqInfo, time);
             record.setComponentId(IdUtil.simpleUUID());
             webComponentMapper.insertSelective(record);
         }
    }
}